package com.beiye.drivertransport.examination.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.bean.DaikaoAndHavetestCourseBean;
import com.beiye.drivertransport.bean.QuestionInvestigationBean;

import java.util.List;


/**
 * Created by zhangyipeng on 16/6/30.
 */
public class QuestionTopicAdapter extends RecyclerView.Adapter<QuestionTopicAdapter.TopicViewHolder> {

    private Context mContext;
    private final LayoutInflater inflater;
    private final Resources resources;
    private List<QuestionInvestigationBean.DataBean.UqItemListBean> datas;

    public void setDatas(List<QuestionInvestigationBean.DataBean.UqItemListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public QuestionTopicAdapter(Context mContext) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        resources = mContext.getResources();

    }

    @Override
    public QuestionTopicAdapter.TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_topic_new, parent, false);
        return new TopicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final QuestionTopicAdapter.TopicViewHolder holder, final int position) {

        holder.tv_id.setText((position + 1) + "");

      //  holder.tv_id.setBackgroundResource(R.drawable.bg_topic_no);
//        if (prePosition == position) {
//            holder.tv_id.setBackgroundResource(R.drawable.bg_topic_no);
//            holder.tv_id.setTextColor(Color.parseColor("#b3afaf"));
//
//        }
        if (datas != null && datas.get(position).isSelect()) {//表示已经做过

            holder.tv_id.setBackgroundResource(R.drawable.bg_topic_blue);
        }else{
            holder.tv_id.setBackgroundResource(R.drawable.bg_topic_no);
        }
//        if (curPosition == position) {
//            holder.tv_id.setBackgroundResource(R.drawable.bg_topic_ok);
//            holder.tv_id.setTextColor(Color.parseColor("#a3a5a9"));
//        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas != null && datas.get(position).isSelect()) {//表示已经做过
                    listener.onClick(holder, position);
                }
            }
        });
    }

    private OnTopicClickListener listener;

    public void setOnTopicClickListener(OnTopicClickListener listener) {
        this.listener = listener;
    }

    private int curPosition;

    public void notifyCurPosition(int curPosition) {
        this.curPosition = curPosition;
        notifyItemChanged(curPosition);
    }

    private int prePosition;

    public void notifyPrePosition(int prePosition) {
        this.prePosition = prePosition;
        notifyItemChanged(prePosition);
    }


    public interface OnTopicClickListener {
        void onClick(QuestionTopicAdapter.TopicViewHolder holder, int position);
    }

    private int num;

    public void setDataNum(int num) {
        this.num = num;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return num;
    }


    public static class TopicViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_id;

        public TopicViewHolder(View itemView) {
            super(itemView);
            tv_id = (TextView) itemView.findViewById(R.id.tv_id);
        }
    }
}
